﻿''
''Copyright Zac McKenzie 2012
''

Imports System.Xml
Imports System.Xml.Linq
Imports System.Linq

' NOTE: You can use the "Rename" command on the context menu to change the class name "Service1" in both code and config file together.
Public Class bwAccessor
    'Implements ITimers

    Public Shared timerList As New List(Of scheduledEvent)
    Public tn As New telnetIF
    Public Function GetTimers(ByVal ipAddress As String, ByVal telNetCommand As String) As List(Of scheduledEvent) ' Implements ITimers.GetTimers

        Dim RecvString As String = tn.doCommand(ipAddress, telNetCommand)

        If RecvString = "" Then
            Throw New DataException("No data received from BeyonWiz " & ipAddress)
            Exit Function
        End If
        If RecvString.IndexOf("</BeyonWiz>") = -1 Then
            Throw New DataException("Didn't get all the data from BeyonWiz " & ipAddress)
            Exit Function
        End If
        ' Send recieved bytes to the output text box
        RecvString = RecvString.Substring(RecvString.IndexOf("<BeyonWiz>") - 1)
        RecvString = RecvString.Substring(1, RecvString.IndexOf("</BeyonWiz>") + "</BeyonWiz>".Length)

        RecvString = RecvString.Replace("&#x05;", "")

        '/tmp/mnt/index.txt

        Dim doc As System.Xml.Linq.XDocument = XDocument.Parse(RecvString)

        For Each t As XElement In doc.Descendants("Timer")
            Dim p = New scheduledEvent(t)
            timerList.Add(p)
        Next

        timerList = (From se As scheduledEvent In timerList Order By se.startTime Select se).ToList

        Return timerList
    End Function

    Public Function getSvc(ByVal ipAddress As String, ByVal telNetCommand As String) As String

        Dim RecvString As String = tn.doCommand(ipAddress, "cat /tmp/config/svc.dat")
        Return RecvString
    End Function

End Class
